
#include "../common/improve_div.h"
#include "../common/dynamic_int_arr.h"
#include "../common/modularity_mat.h"
#include "../common/mem.h"

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
	const char* adjacency_mat_file;
	const char* group_file;
	const char* first_group_file;
	sparse_matrix_arr* adj_matrix;
	elem dQue = 0;
	dynamic_int_arr* group_indices;
	dynamic_int_arr* first_group_indices;
	int i,j;
	int group_size;
	int* S;
	ModularityMatrix* Bg;

	if (argc != 4)
		nice_exit("Usage: %s <adjacency-mat-file> <group-file> <first-group-file>\n\n", argv[0]);

	/*  Take arguments */
	adjacency_mat_file = argv[1];
	group_file = argv[2];
	first_group_file = argv[3];

	/*  Read files */
	adj_matrix = sparse_matrix__read_from_file(adjacency_mat_file, -1);
	group_indices = dynamic_int_arr__create_from_file(group_file);
	first_group_indices = dynamic_int_arr__create_from_file(first_group_file);

	/*  Create vector "S" */
	group_size = group_indices->count;
	S = mem_alloc(group_size, sizeof(int));
	j = 0;
	for (i = 0; i < group_size; i++)
	{
		while (j < first_group_indices->count &&
			first_group_indices->values[j] < group_indices->values[i])
			j++;

		if (first_group_indices->values[j] == group_indices->values[i])
			S[i] = 1;
		else
			S[i] = -1;
	}

	Bg = make_modularity_mat(adj_matrix, group_indices->values, group_size);

	{
		int i;
		int first_group_sign;

		dQue = improve_div(Bg, S);

		printf("%f\n", dQue);

		first_group_sign = S[0];

		for(i = 0; i < group_size; i++)
		{
			if (S[i] == first_group_sign)
				printf("%d ", group_indices->values[i]);
		}
		printf("\n");

		for(i = 0; i < group_size; i++)
		{
			if (S[i] != first_group_sign)
				printf("%d ", group_indices->values[i]);
		}
		printf("\n");
	}

	free_all();

	return 0;
}
